<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:xf="http://www.w3.org/2002/xforms"
 xmlns:ev="http://www.w3.org/2001/xml-events"
 xmlns:js="http://www.formsplayer.com/inlineScript"
>
	<head>
		<title>Update Twitter</title>

		<script type="text/javascript" src="gadgets/common/scripts/events.js">/**/</script>

		<script type="text/javascript" src="gadgets/common/scripts/uri.js">/**/</script>

		<script type="text/javascript" src="gadgets/common/scripts/style.js">/**/</script>

		<link rel="stylesheet" href="gadgets/common/style/gadgets.css" type="text/css" media="screen" />

		<link rel="stylesheet" href="post.css" type="text/css" media="screen" />

		<xf:model id="mdl-gadgets">
			<xf:instance id="inst-gadget">
				<gadget xmlns="">
					<type />

					<value />
				</gadget>
			</xf:instance>

			<xf:instance id="inst-authorisation">
				<authorisation xmlns="" />
			</xf:instance>

			<xf:instance id="inst-tweet">
				<tweet xmlns="">
					<content />
					<length>0</length>
					<ok>true</ok>
				</tweet>
			</xf:instance>

			<xf:bind nodeset="instance('inst-tweet')">
				<xf:bind id="tweet" nodeset="content" />

				<xf:bind id="tweet-length" nodeset="length" calculate="string-length(../content)" />

				<xf:bind id="tweet-ok" nodeset="ok" calculate="if(../length &lt;= 140, 'true', 'false')" relevant=". = 'true'" />
			</xf:bind>

			<xf:submission id="sbm-twitter" method="post" replace="none" validate="false" serialize="false">
				<!--
					HACK: I am having to double URI-encode the status parameter here, because WireShark has
					      shown me that Ubiquity formsPlayer must be performing its own URI-decode step as
					      part of its submission processing somewhere.
				-->
				<xf:resource value="concat('http://twitter.com/statuses/update.xml?source=hubbub&amp;status=', js:URI_encode(js:URI_encode(string(instance('inst-tweet')/content))))" />

				<xf:header>
					<xf:name value="'Authorization'" />
					<xf:value value="instance('inst-authorisation')" />
				</xf:header>
			</xf:submission>

			<xf:instance id="inst-ready">
				<ready xmlns="">true</ready>
			</xf:instance>

			<xf:instance id="inst-show">
				<show xmlns="">true</show>
			</xf:instance>

			<xf:instance id="inst-scripts">
				<scripts xmlns="">
					<retval />
				</scripts>
			</xf:instance>

			<xf:bind id="script-retval" nodeset="instance('inst-scripts')/retval" />

			<xf:action ev:event="xforms-ready">
				<xf:setvalue bind="script-retval" value="js:STYLE_set('originptr_t', 'display', 'block')" />

				<xf:setvalue bind="script-retval" value="js:EVENTS_initNotifyHandlers()" />

				<xf:setvalue bind="script-retval" value="js:EVENTS_initUpdateHandler()" />

				<xf:setvalue bind="script-retval" value="js:EVENTS_send('backplane-request-authorisation', 'twitter')" />
			</xf:action>

			<xf:action ev:event="backplane-notify-authorisation">
				<xf:action if="instance('inst-ready') = 'true'">
					<xf:setvalue ref="instance('inst-ready')" value="'false'" />

					<xf:setvalue bind="script-retval" value="js:EVENTS_send('backplane-ready')" />
				</xf:action>
			</xf:action>

			<xf:action ev:event="backplane-update">
				<xf:setvalue bind="tweet" value="if(instance('inst-gadget')/value != '', concat('@', instance('inst-gadget')/value, ' ', .), .)" />

				<xf:action if="instance('inst-show') = 'true'">
					<xf:setvalue ref="instance('inst-show')" value="'false'" />

					<xf:setvalue bind="script-retval" value="js:EVENTS_send('backplane-show')" />
				</xf:action>
			</xf:action>

			<xf:action ev:observer="sbm-twitter" ev:event="xforms-submit">
				<xf:toggle case="view-status-busy" />
			</xf:action>

			<xf:action ev:observer="sbm-twitter" ev:event="xforms-submit-error">
				<xf:toggle case="view-status-ok" />
			</xf:action>

			<xf:action ev:observer="sbm-twitter" ev:event="xforms-submit-done">
				<xf:setvalue bind="tweet" value="''" />

				<xf:toggle case="view-status-ok" />
			</xf:action>
		</xf:model>
	</head>

	<body ondragenter="event.returnValue = false;" ondragover="event.returnValue = false;" ondrop="event.returnValue = false;">
		<div class="buffer-top">
			<div class="origin-pointer" id="originptr_t"></div>
		</div>

		<span class="buffer-left">
			<div class="origin-pointer" id="originptr_l"></div>
		</span>

		<span class="gadget">
			<xf:trigger appearance="minimal">
				<xf:label class="close-button">
					<img src="gadgets/common/images/button_fg.png" alt="Close" class="button16" />
				</xf:label>

				<xf:setvalue ev:event="DOMActivate" bind="script-retval" value="js:EVENTS_send('backplane-close')" />
			</xf:trigger>

			<h1>
				<img src="gadgets/twitter/icon50.png" alt="Twitter" class="service-icon" />
				Update Twitter
			</h1>

			<xf:switch>
				<xf:case id="view-status-ok"></xf:case>

				<xf:case id="view-status-busy">
					<img src="gadgets/common/images/status-busy.gif" class="status-indicator" />
				</xf:case>
			</xf:switch>

			<div class="content">
				<xf:output value="140 - number(instance('inst-tweet')/length)" />

				<xf:textarea bind="tweet" incremental="true" />

				<xf:submit bind="tweet-ok" submission="sbm-twitter" appearance="minimal">
					<xf:label class="button73">
						Update
					</xf:label>
				</xf:submit>
			</div>
		</span>
	</body>
</html>
